﻿using UnityEngine;

public class SingletonRoot : Singleton_Unity<SingletonRoot>
{
    
}

public abstract class Singleton_Unity<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;

    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject obj = new GameObject("Singleton_" + typeof(T).ToString());
                //to niyi
                if (Application.isPlaying)
                    GameObject.DontDestroyOnLoad(obj);
                instance = obj.AddComponent<T>();
                obj.transform.SetParent(SingletonRoot.Instance.transform);
            }

            return instance;
        }
    }
}